 <!-- 企业登录页面 -->
<template>
  <div id="register">
    <div class="register-box">
      <!-- tab切换 -->
      <div class="typeChange">
        <div
          class="typeTab"
          :class="typeTabIndex == 1 ? 'active' : ''"
          @click="clicktypeTab(1)"
        >
          登录
        </div>
        <div
          class="typeTab"
          :class="typeTabIndex == 2 ? 'active' : ''"
          @click="clicktypeTab(2)"
        >
          注册
        </div>
      </div>
      <!-- 普通用户登录 -->
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="企业名称" prop="name" v-if="typeTabIndex == 2">
          <el-input v-model="ruleForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="企业邮箱" prop="email">
          <el-input v-model="ruleForm.email"></el-input>
        </el-form-item>
        <el-form-item label="营业执照" prop="license" v-if="typeTabIndex == 2">
          <el-input v-model="ruleForm.license" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="企业类型" prop="type" v-if="typeTabIndex == 2">
          <el-select v-model="ruleForm.type" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="密码" prop="pass">
          <el-input
            type="password"
            v-model="ruleForm.pass"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="验证码" prop="code">
          <el-input
            class="code-input"
            type="password"
            v-model="ruleForm.code"
            autocomplete="off"
          ></el-input>
          <div
            class="codeImg"
            @click="getcode()"
            style="width: 100px; height: 50px; background: white; float: right"
          >
            <img class="autoCode" :src="codeImg" alt="验证码" />
          </div>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="LoginForm('ruleForm')"
            :plain="true"
            v-if="typeTabIndex == 1"
            >登录</el-button
          >
          <el-button
            type="primary"
            @click="RegisterForm('ruleForm')"
            :plain="true"
            v-if="typeTabIndex == 2"
            >注册</el-button
          >
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import axios from "../request/axios";
import companyModel from "../request/index";
import cookie from "js-cookie";
export default {
  data() {
    //注册验证
    var name = (rule, value, callback) => {
      if (!value) {
        return callback(new Error("企业名称不能为空"));
      } else {
        if (/^[\u4e00-\u9fa5A-Za-z]{6,20}$/.test(value)) {
          return callback();
        } else {
          return callback(new Error("企业名称不得包含非法字符"));
        }
      }
    };
    var email = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入邮箱"));
      } else {
        if (/^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/.test(value)) {
          callback();
        } else {
          callback(new Error("请输入正确的邮箱"));
        }
      }
    };
    var license = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请上传营业执照"));
      } else {
        return callback();
      }
    };
    var type = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请选择企业类型"));
      } else {
        return callback();
      }
    };
    var pass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else {
        if (/^[a-z0-9]{5,12}$/.test(value)) {
          callback();
        } else {
          callback(new Error("密码必须为字母开头6-12位数字字母组成"));
        }
      }
    };
    var code = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入验证码"));
      } else {
        return callback();
      }
    };
    return {
      // 验证码图片-
      codeImg: "",
      // 验证规则
      ruleForm: {
        name: "",
        email: "",
        license: "",
        type: "",
        pass: "",
        code: "",
      },
      rules: {
        name: [{ validator: name, trigger: "blur" }],
        email: [{ validator: email, trigger: "blur" }],
        license: [{ validator: license, trigger: "blur" }],
        type: [{ validator: type, trigger: "blur" }],
        pass: [{ validator: pass, trigger: "blur" }],
        code: [{ validator: code, trigger: "blur" }],
      },
      // 企业类型
      options: [
        {
          value: "选项1",
          label: "IT",
        },
        {
          value: "选项2",
          label: "医疗",
        },
        {
          value: "选项3",
          label: "金融",
        },
      ],
      //tab切换下标
      typeTabIndex: 1,
    };
  },
  //页面的事件和方法
  methods: {
    // 登录验证
    LoginForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          companyModel
            .companyLogin({
              userName: this.ruleForm.email,
              password: this.ruleForm.pass,
              txt: this.ruleForm.code,
            })
            .then((res) => {
              if (res.data.success == true) {
                this.$message.success("登录成功!");
                this.$router.push({ path: "/companyhome" }); //登录成功跳转企业首页
                let userinfo = res.data.result;
                cookie.set("satoken", userinfo.token);
                userinfo = JSON.stringify(userinfo);
                window.localStorage.setItem("userinfo", userinfo);
                console.log(userinfo);
              } else {
                this.$message.error(res.data.message);
              }
            });
        } else {
          this.$message.error("请检查您的用户名和密码是否正确!");
          return false;
        }
      });
    },

    //注册验证
    RegisterForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          companyModel
            .companyRegister({
              companyName: this.ruleForm.name,
              companyEmail: this.ruleForm.email,
              businessLicense: this.ruleForm.license,
              type: this.type,
              password: this.ruleForm.pass,
              txt: this.ruleForm.code,
            })
            .then((res) => {
              if (res.data.success == true) {
                this.$message.success("注册成功!");
                this.typeTabIndex = 1;
              } else {
                this.$message.error(res.data.message);
              }
            });
        } else {
          this.$message.error("网络错误！");
          return false;
        }
      });
    },

    //点击获取验证码
    getcode() {
      this.codeImg =
        axios.defaults.baseURL +
        "file/getImg" +
        "?data = " +
        new Date().getTime();
    },

    //重置信息
    resetForm(ruleForm) {
      this.$refs[ruleForm].resetFields();
    },

    //tab切换
    clicktypeTab(i) {
      this.typeTabIndex = i;
    },
  },
  //生命周期（页面加载时执行）
  created() {
    // 页面加载时生成验证码
    this.codeImg =
      axios.defaults.baseURL +
      "file/getImg" +
      "?data = " +
      new Date().getTime();
  },
};
</script>

<style lang="scss" scoped>
@import "../css/RegisterAndLogin.scss";
.autoCode {
  width: 100%;
  height: 100%;
}
</style>

